package com.atguigu.dga230315.governance.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringBeanProvider implements ApplicationContextAware {

    static ApplicationContext context;

    /**
     * 这个方法给我们提供了一个ApplicationContext应用程序的上下文对象 其实也就是容器
     * 这个方法主要就是为了获取容器
     * @param applicationContext the ApplicationContext object to be used by this object
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    //从名称获取容器中的组件
    public  static <T> T getBean(String name,Class<T> aClass){
        return context.getBean(name,aClass);
    }

}
